// 加载Express模块
const express = require('express');

// 加载MySQL模块
const mysql = require('mysql');

// 加载bodyParser模块
const bodyParser = require('body-parser');

// 加载MD5模块
const md5 = require('md5');

// 创建MySQL连接池
const pool = mysql.createPool({
  host: '127.0.0.1',   //MySQL服务器地址
  port: 3306,          //MySQL服务器端口号
  user: 'root',        //数据库用户的用户名
  password: '1234',        //数据库用户密码
  database: 'dhl',    //数据库名称
  connectionLimit: 20, //最大连接数
  charset: 'utf8'      //数据库服务器的编码方式
});

// 创建服务器对象
const server = express();

server.use(bodyParser.urlencoded({
  extended: false
}));


// 加载CORS模块
const cors = require('cors');

// 使用CORS中间件
server.use(cors({
  origin: ['http://localhost:8080', 'http://127.0.0.1:8080']
}));

// 获取所有文章分类的接口
server.get('/index1', (req, res) => {
  // SQL语句以获取文章分类表的数据
  let sql = 'SELECT * FROM dhl_index1';
  // 执行SQL语句
  pool.query(sql, (error, results) => {
    if (error) throw error;
    res.send({ message: 'ok', code: 200, results: results });
  });
});
server.get('/index2', (req, res) => {
  // SQL语句以获取文章分类表的数据
  let sql = 'SELECT * FROM dhl_index2';
  // 执行SQL语句
  pool.query(sql, (error, results) => {
    if (error) throw error;
    res.send({ message: 'ok', code: 200, results: results });
  });
});
server.get('/index3', (req, res) => {
  // SQL语句以获取文章分类表的数据
  let sql = 'SELECT * FROM dhl_index3';
  // 执行SQL语句
  pool.query(sql, (error, results) => {
    if (error) throw error;
    res.send({ message: 'ok', code: 200, results: results });
  });
});
server.get('/index4', (req, res) => {
  // SQL语句以获取文章分类表的数据
  let sql = 'SELECT * FROM dhl_index4';
  // 执行SQL语句
  pool.query(sql, (error, results) => {
    if (error) throw error;
    res.send({ message: 'ok', code: 200, results: results });
  });
});
server.get('/index5', (req, res) => {
  // SQL语句以获取文章分类表的数据
  let sql = 'SELECT * FROM dhl_index5';
  // 执行SQL语句
  pool.query(sql, (error, results) => {
    if (error) throw error;
    res.send({ message: 'ok', code: 200, results: results });
  });
});
server.get('/index6', (req, res) => {
  // SQL语句以获取文章分类表的数据
  let sql = 'SELECT * FROM dhl_index6';
  // 执行SQL语句
  pool.query(sql, (error, results) => {
    if (error) throw error;
    res.send({ message: 'ok', code: 200, results: results });
  });
});
server.get('/details', (req, res) => {
  console.log(req.query);
  let did=req.query.did
  let data=req.query.data.split('ta')[1]
  // SQL语句以获取文章分类表的数据
  let sql = `SELECT * FROM dhl_index${data} WHERE did=${did}`;
  // 执行SQL语句
  pool.query(sql, (error, results) => {
    if (error) throw error;
    res.send({ message: 'ok', code: 200, results: results });
  });
});

// 登录页面
server.post('/login/:ph/:pw',(req,res)=>{
  let jg=req.params
  console.log(jg);
  let ph=jg.ph.split('=')[1]
  let pw=jg.pw.split('=')[1]
  console.log(ph,pw);
  let sql=`select * from dhl_login where ph='${ph}' and pw='${pw}'`
  pool.query(sql,(err,result)=>{
    if(err) throw err
    if(result.length==0){
      res.send({message:'err',code:401})
    }else{
      res.send({message:'ok',code:200})
    }
  })
})
// 注册页面
server.post('/register/:ph/:pw',(req,res)=>{
  let jg=req.params
  let ph=jg.ph.split('=')[1]
  let pw=jg.pw.split('=')[1]
  console.log(ph,pw)
  let sql=`insert into dhl_login set ?`
  pool.query(sql,{ph:ph,pw:pw},(err,result)=>{
    if(err) throw err
    console.log(result);
    if(result.affectedRows){
      res.send({message:'ok',code:200})
    }else{
      res.send({message:'err',code:401})
    }
  })
})
// 查询页面
server.get('/register/:ph',(req,res)=>{
  let jg=req.params
  let ph=jg.ph.split('=')[1]
  let sql=`select * from dhl_login where ph=?`
  pool.query(sql,[ph],(err,result)=>{
    if(err) throw err
    console.log(result);
    if(result.length==0){
      res.send({message:'ok',code:200})
    }else{
      res.send({message:'err',code:401})
    }
  })
})

// 指定服务器对象监听的端口号
server.listen(3000, () => {
  console.log('server is running...');
});